//
// Created by SXY on 2023/8/1.
//

#include "driver_ui.h"
#include "string.h"
#include "stdio.h"
#include "bsp_can.h"
#include "math.h"
#include "task_lostcounter.h"

extern ext_student_interactive_header_data_t student_interactive_header;
UI_Info_t ui_info;
/****************     画图数据初始化     ****************/
ext_client_custom_graphic_delete_t Graphic_Delete_Graph;
ext_client_custom_graphic_single_t Graphic_Single_Graph;
ext_client_custom_graphic_double_t Graphic_Double_Graph;
ext_client_custom_graphic_five_t Graphic_Five_Graph;
ext_client_custom_graphic_seven_t Graphic_Seven_Graph;
ext_client_custom_character_t Graphic_Character_Graph;

uint8_t Check_Flag = 0;
void clear_all(void)
{
    Graphic_Delete_Graph.operate_type = 2;
    student_interactive_header.data_cmd_id = STU_CUSTOM_DELETE_PICTURE_ID;
    data_packet_pack (STU_INTERACTIVE_ID, (uint8_t*)&Graphic_Delete_Graph, sizeof(Graphic_Delete_Graph), student_interactive_header);
}

void show_line(graphic_data_struct_t *g_line, char *name, uint32_t _operation, uint32_t _color, uint32_t _layer,uint32_t _width,
               uint32_t start_x, uint32_t start_y, uint32_t details_d, uint32_t details_e)
{
    memcpy(g_line->graphic_name,name,3);
    g_line->operate_type = _operation;//1增加2修改3删除
    g_line->graphic_type = 0;//1是矩形,0是直线,2是整圆
    g_line->layer = _layer;
    g_line->color = _color;
    g_line->details_a = 0;
    g_line->details_b = 0;
    g_line->width = _width;
    g_line->start_x = start_x;
    g_line->start_y = start_y;
    g_line->details_c = 0;
    g_line->details_d = details_d;
    g_line->details_e = details_e;
}

/*
 * 准星绘制
 * 每次绘制一个图形（一条线）
 */
void Draw_front_sight(void)
{
    static int _draw_fs_case = 0;
#if NO_HERO == 0
    const uint32_t  _sight_offset_x = 53;
    const uint32_t  _sight_offset_y = 540;
//    const uint32_t  shoot_offset_x = 5;
#elif NO_HERO == 1
    const uint32_t  _sight_offset_x = 53;
    const uint32_t  _sight_offset_y = 640;
    const uint32_t  shoot_offset_x = 5;
#endif

    // TODO: 准星绘制优化、待测试
//    show_line(&Graphic_Seven_Graph.grapic_data_struct[0], "F0", 0, 3, 0, 2, 617, 0, 694, 111);
//    show_line(&Graphic_Seven_Graph.grapic_data_struct[1], "F1", 0, 3, 0, 2, 617, 0, 694, 111);
//    show_line(&Graphic_Seven_Graph.grapic_data_struct[2], "F2", 0, 3, 0, 2, 617, 0, 694, 111);
//    show_line(&Graphic_Seven_Graph.grapic_data_struct[3], "F3", 0, 3, 0, 2, 617, 0, 694, 111);
//    show_line(&Graphic_Seven_Graph.grapic_data_struct[4], "F4", 0, 3, 0, 2, 617, 0, 694, 111);
//    show_line(&Graphic_Seven_Graph.grapic_data_struct[5], "F5", 0, 3, 0, 2, 617, 0, 694, 111);
//    show_line(&Graphic_Seven_Graph.grapic_data_struct[6], "F6", 0, 3, 0, 2, 617, 0, 694, 111);

    Graphic_Single_Graph.grapic_data_struct.graphic_type = 0;   //直线
    Graphic_Single_Graph.grapic_data_struct.layer = 0;
    Graphic_Single_Graph.grapic_data_struct.color = 3;          //橙色
    Graphic_Single_Graph.grapic_data_struct.details_a = 0;
    Graphic_Single_Graph.grapic_data_struct.details_b = 0;
    Graphic_Single_Graph.grapic_data_struct.width = 2;
    Graphic_Single_Graph.grapic_data_struct.details_c = 0;

    _draw_fs_case = (_draw_fs_case + 1) % 12;
    switch (_draw_fs_case)
    {
        case 0:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "F0");
            Graphic_Single_Graph.grapic_data_struct.start_x = 898;
            Graphic_Single_Graph.grapic_data_struct.start_y = 512;
            Graphic_Single_Graph.grapic_data_struct.details_d = 898;
            Graphic_Single_Graph.grapic_data_struct.details_e = 568;
            break;
        case 1:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "F1");
            Graphic_Single_Graph.grapic_data_struct.start_x = 989;
            Graphic_Single_Graph.grapic_data_struct.start_y = 512;
            Graphic_Single_Graph.grapic_data_struct.details_d = 989;
            Graphic_Single_Graph.grapic_data_struct.details_e = 568;
            break;
        case 2:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "F2");
            Graphic_Single_Graph.grapic_data_struct.start_x = 950 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.start_y = 190;
            Graphic_Single_Graph.grapic_data_struct.details_d = 950 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.details_e = _sight_offset_y + 60;
            break;
        case 3:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "F3");
            Graphic_Single_Graph.grapic_data_struct.start_x = 960 - _sight_offset_x - 10 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.start_y = _sight_offset_y - 50;
            Graphic_Single_Graph.grapic_data_struct.details_d = 960 + _sight_offset_x -10 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.details_e = _sight_offset_y - 50;
            break;
        case 4:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "F4");
            Graphic_Single_Graph.grapic_data_struct.start_x = 960 - _sight_offset_x -3 -shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.start_y = _sight_offset_y - 100;
            Graphic_Single_Graph.grapic_data_struct.details_d = 960 + _sight_offset_x - 17 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.details_e = _sight_offset_y - 100;
            break;
        case 5:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "F5");
            Graphic_Single_Graph.grapic_data_struct.color = 4;
            Graphic_Single_Graph.grapic_data_struct.start_x = 960 - _sight_offset_x + 4 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.start_y = _sight_offset_y - 155;
            Graphic_Single_Graph.grapic_data_struct.details_d = 960 + _sight_offset_x - 24 -shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.details_e = _sight_offset_y - 155;
            break;
        case 6:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "F6");
            Graphic_Single_Graph.grapic_data_struct.start_x = 960 - _sight_offset_x + 11 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.start_y = _sight_offset_y - 200;
            Graphic_Single_Graph.grapic_data_struct.details_d = 960 + _sight_offset_x - 31 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.details_e = _sight_offset_y - 200;
            break;

        case 7:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "FB");
            Graphic_Single_Graph.grapic_data_struct.color = 0;
            Graphic_Single_Graph.grapic_data_struct.start_x = 960 - _sight_offset_x + 4 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.start_y = _sight_offset_y - 120;
            Graphic_Single_Graph.grapic_data_struct.details_d = 960 + _sight_offset_x - 24 - shoot_offset_x;
            Graphic_Single_Graph.grapic_data_struct.details_e = _sight_offset_y - 120;
            break;
        case 8:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "FC");
            Graphic_Single_Graph.grapic_data_struct.color = 0;
            Graphic_Single_Graph.grapic_data_struct.start_x =  950 - shoot_offset_x - 26;
            Graphic_Single_Graph.grapic_data_struct.start_y = _sight_offset_y - 125 - 8;
            Graphic_Single_Graph.grapic_data_struct.details_d =  950 - shoot_offset_x - 26;
            Graphic_Single_Graph.grapic_data_struct.details_e = _sight_offset_y - 125 + 13;
            break;
        case 9:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "FD");
            Graphic_Single_Graph.grapic_data_struct.color = 0;
            Graphic_Single_Graph.grapic_data_struct.start_x =  967;
            Graphic_Single_Graph.grapic_data_struct.start_y = _sight_offset_y - 125 - 8;
            Graphic_Single_Graph.grapic_data_struct.details_d =  967;
            Graphic_Single_Graph.grapic_data_struct.details_e = _sight_offset_y - 125 + 13;
            break;
        case 10:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "F0");
            Graphic_Single_Graph.grapic_data_struct.start_x = 617;
            Graphic_Single_Graph.grapic_data_struct.start_y = 0;
            Graphic_Single_Graph.grapic_data_struct.details_d = 674;
            Graphic_Single_Graph.grapic_data_struct.details_e = 131;
            break;
        case 11:
            strcpy(Graphic_Single_Graph.grapic_data_struct.graphic_name, "F1");
            Graphic_Single_Graph.grapic_data_struct.start_x = 1353;
            Graphic_Single_Graph.grapic_data_struct.start_y = 0;
            Graphic_Single_Graph.grapic_data_struct.details_d = 1296;
            Graphic_Single_Graph.grapic_data_struct.details_e = 131;
            break;

        default :
            _draw_fs_case = -1;
            break;
    }

    student_interactive_header.data_cmd_id = STU_CUSTOM_ONE_PICTURE_ID;
    data_packet_pack(STU_INTERACTIVE_ID, (uint8_t*)&Graphic_Single_Graph, sizeof(Graphic_Single_Graph), student_interactive_header);
}

/*
 * 超级电容条绘制
 * 使用电容时颜色为红蓝主色，反之为黑色
 * 绘制两个图形
 */
void Update_SC_Bar(graphic_data_struct_t *graphic_progress){
    if (ui_info.SC_flag)
    {
        graphic_progress->color = 0;
    }
    else
    {
        graphic_progress->color = 7;
    }
    // 电容条
    strcpy(graphic_progress->graphic_name, "S0");
    graphic_progress->graphic_type = 1;   //矩形
    graphic_progress->layer = 1;
    graphic_progress->details_a = 0;
    graphic_progress->details_b = 0;
    graphic_progress->details_c = 0;
    graphic_progress->width = 10;
    graphic_progress->start_x = 800;
    graphic_progress->start_y = 35;
    graphic_progress->details_d = 800 + ui_info.SC_proportion * 345.f;
    graphic_progress->details_e = 25;
}
void Draw_SC_bar(graphic_data_struct_t *graphic_progress, graphic_data_struct_t *graphic_container)
{
    if (ui_info.SC_flag)
    {
        graphic_progress->color = 0;
    }
    else
    {
        graphic_progress->color = 7;
    }
    graphic_container->color = 8;
    // 电容条
    strcpy(graphic_progress->graphic_name, "S0");
    graphic_progress->graphic_type = 1;   //矩形
    graphic_progress->layer = 1;
    graphic_progress->details_a = 0;
    graphic_progress->details_b = 0;
    graphic_progress->details_c = 0;
    graphic_progress->width = 10;
    graphic_progress->start_x = 800;
    graphic_progress->start_y = 35;
    graphic_progress->details_d = 800 + ui_info.SC_proportion * 345.f;
    graphic_progress->details_e = 25;
    // 外框
    strcpy(graphic_container->graphic_name, "S1");
    graphic_container->graphic_type = 1;   //矩形
    graphic_container->layer = 1;
    graphic_container->details_a = 0;
    graphic_container->details_b = 0;
    graphic_container->details_c = 0;
    graphic_container->width = 2;
    graphic_container->start_x = 795;
    graphic_container->start_y = 40;
    graphic_container->details_d = 1195;
    graphic_container->details_e = 20;
}

char err_state2[13][12]={"JUDGE","FEED","WHE_0","WHE_1","WHE_2","WHE_3","L_FRIC","R_FRIC","H_FRIC","PITCH","YAW","SuperC","VISION"};

void get_err_str(uint8_t err_code){
    int flag = FALSE;
    for(int i = 0;i < 14;i++){
        if(err_code == i + 1){
            strcat(Graphic_Character_Graph.data,err_state2[i]);
            strcat(Graphic_Character_Graph.data,"\n\n");
            flag = TRUE;
            break;
        }
    }
    if(flag == FALSE)
        strcat(Graphic_Character_Graph.data,"OK\n\n");
}

/*
 * 左侧字符信息更新
 *            -----------------------------------------------
 * err:      |  正常显示OK 否则按优先级显示错误发生来源           |
 * chassis:  |  底盘状态    F 跟随   G 陀螺    L 锁定    R 关控 |
 * pitch:    |  pitch 角度                                    |
 * Fri-Auto: |  摩擦轮转速等级-自瞄模式                         |
 *            -----------------------------------------------
 */
uint8_t supercap_flag = 0;
int Draw_Character = 0;
void Draw_robot_status_update()
{
    Draw_Character = (Draw_Character + 1) % 20;
    for (int i = 0; i < 30; i++)
        Graphic_Character_Graph.data[i] = '\0';
    Graphic_Character_Graph.grapic_data_struct.graphic_type = 7;    // 字符
    Graphic_Character_Graph.grapic_data_struct.layer = 2;
    Graphic_Character_Graph.grapic_data_struct.color = 3;           // 橙色
    Graphic_Character_Graph.grapic_data_struct.details_a = 20;    // 字体大小
    Graphic_Character_Graph.grapic_data_struct.details_b = 9;       // 字符长度
    Graphic_Character_Graph.grapic_data_struct.width = 2;
    Graphic_Character_Graph.grapic_data_struct.details_c = 0;
    Graphic_Character_Graph.grapic_data_struct.details_d = 0;
    Graphic_Character_Graph.grapic_data_struct.details_e = 0;
    if(Draw_Character % 2 == 0) {
        strcpy(Graphic_Character_Graph.grapic_data_struct.graphic_name, "R2");
        Graphic_Character_Graph.grapic_data_struct.start_x = 250;
        Graphic_Character_Graph.grapic_data_struct.start_y = 840;
        if (Unlimit_Flag)
            strcat(Graphic_Character_Graph.data, "ON\n\n");
        else if (Unlimit_Flag == 0)
            strcat(Graphic_Character_Graph.data, "OFF\n\n");

        // err 状态
        get_err_str(ui_info.err_status);

        // 底盘状态
        switch (ui_info.chassisMode) {
            case CHASSIS_MODE_FOLLOW:
                strcat(Graphic_Character_Graph.data, "F\n\n");
                break;
            case CHASSIS_MODE_GYRO:
                Graphic_Character_Graph.grapic_data_struct.color = 6;   //青色
                strcat(Graphic_Character_Graph.data, "S\n\n");
                break;
            case CHASSIS_MODE_LOCK:
            case CHASSIS_MODE_AUTOSHOOT:
                Graphic_Character_Graph.grapic_data_struct.color = 8;   //白色
                strcat(Graphic_Character_Graph.data, "L\n\n");
                break;
            case CHASSIS_MODE_FREE:
                Graphic_Character_Graph.grapic_data_struct.color = 4;   //紫红色
                strcat(Graphic_Character_Graph.data, "R\n\n");
                break;
            default:
                strcat(Graphic_Character_Graph.data, "?\n\n");
                break;
        }

        // pitch角度
        char char_pitch_angle[7];
        sprintf(char_pitch_angle, "%.2f", ui_info.pitch_angle);
        strcat(Graphic_Character_Graph.data, char_pitch_angle);
        strcat(Graphic_Character_Graph.data, "\n\n");

        switch (ui_info.fric_status) {
            case 0:
            case 2:
                strcat(Graphic_Character_Graph.data, "Off");
                break;
            case 1:
                strcat(Graphic_Character_Graph.data, "Ni");
                break;
            case 3:
                strcat(Graphic_Character_Graph.data, "Sh");
                break;
            default:
                strcat(Graphic_Character_Graph.data, "??");
                break;
        }
        switch (ui_info.auto_status) {
            case 0:
                strcat(Graphic_Character_Graph.data, "-Off\n\n");
                break;
            case 1:
                if (ui_info.chassisMode != CHASSIS_MODE_FREE)
                    Graphic_Character_Graph.grapic_data_struct.color = 2;   // 绿色
                strcat(Graphic_Character_Graph.data, "-On\n\n");
                break;
            case 2:
                strcat(Graphic_Character_Graph.data, "-?\n\n");
                break;
            case 3:
                if (ui_info.chassisMode != CHASSIS_MODE_FREE)
                    Graphic_Character_Graph.grapic_data_struct.color = 2;   // 绿色
                strcat(Graphic_Character_Graph.data, "-Au\n\n");
                break;
            default:
                break;
        }
    }
    else if(Draw_Character % 2 == 1) {
        strcpy(Graphic_Character_Graph.grapic_data_struct.graphic_name, "R4");
        Graphic_Character_Graph.grapic_data_struct.start_x = 250;
        Graphic_Character_Graph.grapic_data_struct.start_y = 575;
        //电容开关的情况
        //0为电容处于关闭但正常的状态，1处于电容开启，2处于自动电容开启，3处于error状态
        ///电容现在只有失联状态能够被识别为error（也即can出现问题的时候），最好是能够和硬件
        /// 想出一个办法来在更多的情况下判断出error状态，从而进一步保护车
        if ((GetErrorState() >> SUPERC_LOST_COUNT) & 0x0001) {
            supercap_flag = 3;
        } else if (chassisControlData.disCharging)
            supercap_flag = 1;
        else if (chassisControlData.disCharging_Auto)
            supercap_flag = 2;
        else
            supercap_flag = 0;
        switch (supercap_flag) {
            case 0:
                Graphic_Character_Graph.grapic_data_struct.color = 4;//紫红色
                strcat(Graphic_Character_Graph.data, "\nOFF\n\n");
                break;
            case 1:
                Graphic_Character_Graph.grapic_data_struct.color = 2;   // 绿色
                strcat(Graphic_Character_Graph.data, "\nON\n\n");
                break;
            case 2:
                Graphic_Character_Graph.grapic_data_struct.color = 2;//绿色
                strcat(Graphic_Character_Graph.data, "\nAU\n\n");
                break;
            case 3:
                Graphic_Character_Graph.grapic_data_struct.color = 4;//紫红色
                strcat(Graphic_Character_Graph.data, "\nERR\n\n");
        }
    }
        student_interactive_header.data_cmd_id = STU_CUSTOM_CHARACTER_ID;
        data_packet_pack(STU_INTERACTIVE_ID, (uint8_t*)&Graphic_Character_Graph, sizeof(Graphic_Character_Graph),
                         student_interactive_header);
}

/*
 * 左侧字符信息绘制
 *  -------------------------------------------------------------
 * | err:       正常显示OK 否则按优先级显示错误发生来源                 |
 * | chassis:   底盘状态    F 跟随   G 陀螺    L 锁定    R 关控       |
 * | pitch:     pitch 角度                                        |
 * | Fri-Auto:  摩擦轮转速等级-自瞄模式                               |
 *  -------------------------------------------------------------
 */
void Draw_robot_status_init()
{
    static uint8_t _draw_robot_status_init_case = 0;
    for (int i = 0; i < 30; i++)
        Graphic_Character_Graph.data[i] = '\0';

    Graphic_Character_Graph.grapic_data_struct.graphic_type = 7;    // 字符
    Graphic_Character_Graph.grapic_data_struct.layer = 2;
    Graphic_Character_Graph.grapic_data_struct.color = 3;
    Graphic_Character_Graph.grapic_data_struct.details_a = 20;
    Graphic_Character_Graph.grapic_data_struct.details_b = 9;
    Graphic_Character_Graph.grapic_data_struct.width = 2;
    Graphic_Character_Graph.grapic_data_struct.details_c = 0;
    Graphic_Character_Graph.grapic_data_struct.details_d = 0;
    Graphic_Character_Graph.grapic_data_struct.details_e = 0;

    _draw_robot_status_init_case = (_draw_robot_status_init_case + 1) % 20;
    if (_draw_robot_status_init_case % 4 == 0) {
        strcpy(Graphic_Character_Graph.grapic_data_struct.graphic_name, "R1");
        Graphic_Character_Graph.grapic_data_struct.start_x = 20;
        Graphic_Character_Graph.grapic_data_struct.start_y = 840;
        strcat(Graphic_Character_Graph.data, "INF\n\n");
        strcat(Graphic_Character_Graph.data, "ERR\n\n");
        strcat(Graphic_Character_Graph.data, "MODE\n\n");
        strcat(Graphic_Character_Graph.data, "ANG\n\n");
        strcat(Graphic_Character_Graph.data, "FRI-AU\n\n");
    }
    else if(_draw_robot_status_init_case % 4 == 1){
        strcpy(Graphic_Character_Graph.grapic_data_struct.graphic_name, "R2");
        Graphic_Character_Graph.grapic_data_struct.start_x = 250;
        Graphic_Character_Graph.grapic_data_struct.start_y = 840;
        strcat(Graphic_Character_Graph.data, "OFF\n\n");
        strcat(Graphic_Character_Graph.data, "OK\n\n");
        strcat(Graphic_Character_Graph.data, "F\n\n");
        strcat(Graphic_Character_Graph.data, "0.0\n\n");
        strcat(Graphic_Character_Graph.data, "OFF-OFF\n\n");
    }
    else if(_draw_robot_status_init_case % 4 == 2){
        strcpy(Graphic_Character_Graph.grapic_data_struct.graphic_name, "R3");
        Graphic_Character_Graph.grapic_data_struct.start_x = 20;
        Graphic_Character_Graph.grapic_data_struct.start_y = 575;
        strcat(Graphic_Character_Graph.data, "\nSUP\n\n");
    }
    else if(_draw_robot_status_init_case % 4 == 3){
        strcpy(Graphic_Character_Graph.grapic_data_struct.graphic_name,"R4");
        Graphic_Character_Graph.grapic_data_struct.start_x = 250;
        Graphic_Character_Graph.grapic_data_struct.start_y = 575;
        strcat(Graphic_Character_Graph.data,"\nOFF\n\n");
    }

    student_interactive_header.data_cmd_id = STU_CUSTOM_CHARACTER_ID;
    data_packet_pack(STU_INTERACTIVE_ID, (uint8_t*)&Graphic_Character_Graph, sizeof(Graphic_Character_Graph),
                     student_interactive_header);
}

/*
 * 云台底盘相对角度绘制
 * gimbal:    红蓝主色
 * chassis:   橙色 follow       青色 spin         白色 lock/autoshoot           紫红色 关控
 */
void Draw_yaw_angle(graphic_data_struct_t *graphic_gimbal, graphic_data_struct_t *graphic_chassis, graphic_data_struct_t *graphic_front)
{
    const uint32_t _details_c = 70;
    // gimbal
    strcpy(graphic_gimbal->graphic_name, "Y0");
    graphic_gimbal->graphic_type = 0;       // 直线
    graphic_gimbal->layer = 3;
    graphic_gimbal->color = 0;              // 红蓝主色
    graphic_gimbal->details_a = 0;
    graphic_gimbal->details_b = 0;
    graphic_gimbal->width = 8;              // 线条宽度
    graphic_gimbal->details_c = 0;
    graphic_gimbal->start_x = 1500;         // 圆心x坐标
    graphic_gimbal->start_y = 700;          // 圆心y坐标
    graphic_gimbal->details_d = 1500;
    graphic_gimbal->details_e = 700 + _details_c;

    // chassis
    switch (ui_info.chassisMode) {
        case CHASSIS_MODE_FOLLOW:
            graphic_chassis->color = 3; break;  // 橙色
        case CHASSIS_MODE_GYRO:
            graphic_chassis->color = 3; break;
        case CHASSIS_MODE_LOCK:
        case CHASSIS_MODE_AUTOSHOOT:
            graphic_chassis->color = 8; break;  // 白色
        case CHASSIS_MODE_FREE:
            graphic_chassis->color = 6; break;  // 紫红色
        default:
            graphic_chassis->color = 3;         // 橙色
            break;
    }

    strcpy(graphic_chassis->graphic_name, "Y1");
    graphic_chassis->graphic_type = 4;      // 圆弧
    graphic_chassis->layer = 3;
    graphic_chassis->width = 8;             // 线条宽度
    graphic_chassis->details_c = 0;
    graphic_chassis->start_x = 1500;        // 圆心x坐标
    graphic_chassis->start_y = 700;         // 圆心y坐标
    graphic_chassis->details_d = _details_c;       // x半径长度
    graphic_chassis->details_e = _details_c;       // y半径长度

    float _angle0 = -ui_info.yaw_angle - 0.125f;
    float _angle1 = -ui_info.yaw_angle + 0.125f;
    while (_angle0 < 0) {
        _angle0 += 1;
        _angle1 += 1;
    }

    graphic_chassis->details_a = (uint32_t)(_angle1 * 360.f);
    graphic_chassis->details_b = (uint32_t)(_angle0 * 360.f);

    // chassis-front
    strcpy(graphic_front->graphic_name, "Y2");
    graphic_front->graphic_type = 4;      // 圆形
    graphic_front->layer = 3;
    graphic_front->color = 0;             // 红蓝主色
    graphic_front->width = 8;             // 线条宽度
    graphic_front->details_c = 0;
    graphic_front->start_x = 1500;        // 圆心x坐标
    graphic_front->start_y = 700;         // 圆心y坐标
    graphic_front->details_d = _details_c;       // x半径长度
    graphic_front->details_e = _details_c;       // y半径长度

    graphic_front->details_a = (uint32_t)(_angle0 * 360.f);
    graphic_front->details_b = (uint32_t)(_angle1 * 360.f);
}

void Draw_aim(graphic_data_struct_t *graphic_base)
{
    strcpy(graphic_base->graphic_name, "A1");
    graphic_base->graphic_type = 1;   //矩形
    graphic_base->layer = 4;
    graphic_base->color = 2;
    graphic_base->details_a = 0;
    graphic_base->details_b = 0;
    graphic_base->details_c = 0;
    graphic_base->width = 2;
    graphic_base->start_x = 970;
    graphic_base->start_y = 590;
    graphic_base->details_d = 900;
    graphic_base->details_e = 560;
}

void Add_graphic()
{
    static uint8_t _add_graphic_case = 0;
    //输出字符图形
    Graphic_Character_Graph.grapic_data_struct.operate_type = 1;
    Graphic_Single_Graph.grapic_data_struct.operate_type = 1;       // 增加
    for (int i = 0; i < 2; ++i) {
        Graphic_Double_Graph.grapic_data_struct[i].operate_type = 1;
    }
    for (int i = 0; i < 5; ++i) {
        Graphic_Five_Graph.grapic_data_struct[i].operate_type = 1;
    }
    for (int i = 0; i < 6; ++i) {
        Graphic_Seven_Graph.grapic_data_struct[i].operate_type = 1;
    }

    _add_graphic_case = (_add_graphic_case + 1) % 30;
    //这部分希望能画好得再快一点
    if (_add_graphic_case < 20)
        Draw_front_sight();
    else if(_add_graphic_case < 25) {
        Draw_SC_bar(
                &Graphic_Seven_Graph.grapic_data_struct[0],
                &Graphic_Seven_Graph.grapic_data_struct[1]);
        Draw_yaw_angle(
                &Graphic_Seven_Graph.grapic_data_struct[2],
                &Graphic_Seven_Graph.grapic_data_struct[3],
                &Graphic_Seven_Graph.grapic_data_struct[4]);
        Draw_Circle(&Graphic_Seven_Graph.grapic_data_struct[5]);
        student_interactive_header.data_cmd_id = STU_CUSTOM_SEVEN_PICTURE_ID;
        data_packet_pack(
                STU_INTERACTIVE_ID,
                (uint8_t*)&Graphic_Seven_Graph,
                sizeof(Graphic_Seven_Graph),
                student_interactive_header
        );
    }
    else if(_add_graphic_case < 30)
        Draw_robot_status_init();
}

void update_graphic()
{
    static uint8_t _update_graphic_case = 0;

    Graphic_Character_Graph.grapic_data_struct.operate_type = 2;    // 修改
    Graphic_Single_Graph.grapic_data_struct.operate_type = 2;
    for (int i = 0; i < 2; ++i) {
        Graphic_Double_Graph.grapic_data_struct[i].operate_type = 2;
    }
    for (int i = 0; i < 5; ++i) {
        Graphic_Five_Graph.grapic_data_struct[i].operate_type = 2;
    }
    for (int i = 0; i < 7; ++i) {
        Graphic_Seven_Graph.grapic_data_struct[i].operate_type = 2;
    }

    _update_graphic_case = (_update_graphic_case + 1) % 50;
    if (_update_graphic_case % 4 == 0) {
        Draw_robot_status_update();
        Check_Flag = 1;
    }
    else {
        Update_SC_Bar(&Graphic_Five_Graph.grapic_data_struct[0]);
        Draw_yaw_angle(
                &Graphic_Five_Graph.grapic_data_struct[1],
                &Graphic_Five_Graph.grapic_data_struct[2],
                &Graphic_Five_Graph.grapic_data_struct[3]);
        Draw_Circle(&Graphic_Five_Graph.grapic_data_struct[4]);
        Check_Flag = 2;
        student_interactive_header.data_cmd_id = STU_CUSTOM_FIVE_PICTURE_ID;
        data_packet_pack(
                STU_INTERACTIVE_ID,
                (uint8_t*)&Graphic_Five_Graph,
                sizeof(Graphic_Five_Graph),
                student_interactive_header
        );
    }
}

void Draw_UI(uint8_t clearFlag)
{
    static uint8_t _draw_ui_case = 80;

    if (clearFlag) {
        clear_all();
        _draw_ui_case = 80;
        return;
    }

    // UI 初始化
    if (_draw_ui_case > 50) {
        Add_graphic();
        _draw_ui_case--;
        return;
    }

    _draw_ui_case = (_draw_ui_case + 1) % 50;
    if (_draw_ui_case % 15 == 0) {
        Add_graphic();
    }
    else {
        update_graphic();
    }
}


void Draw_Circle(graphic_data_struct_t *Circle_Frame){
    if(ui_info.Vision_Success){
        Circle_Frame->color = 2;
    }
    else
        Circle_Frame->color = 8;
    strcpy(Circle_Frame->graphic_name,"C0");
    Circle_Frame->graphic_type = 2;
    Circle_Frame->layer = 1;
    Circle_Frame->width = 5;
    Circle_Frame->start_x = 960 - 15;
    Circle_Frame->start_y = 540;
    Circle_Frame->details_c = 252;
}

extern Super_Cap_t superCap;
void update_UI_info()
{
    uint16_t system_err = GetErrorState();
    ui_info.err_status = NUMBERS_OF_COUNT;
    for (uint8_t i = 0; i < NUMBERS_OF_COUNT; i++) {
        if ((system_err >> i) & 0x0001) {
            ui_info.err_status = i;
            break;
        }
    }
    ui_info.chassisMode = gimbalControlData.chassisMode;
    ui_info.fric_status = gimbalStatus.chassisData.heroStatus & 0x01;       // 1：0顺时针 1逆时针          0：0关 1开
    ui_info.auto_status = (gimbalStatus.chassisData.heroStatus >>2) & 0x01;

    ui_info.SC_flag = chassisControlData.superFlag;
    ui_info.SC_proportion = (powf(superCap.scFeedback.V_SC, 2) - 8*8) / (25*25 - 8*8);
    ui_info.pitch_angle = -(gimbalStatus.chassisData.angle) / 100.f;
    ui_info.yaw_angle = GIMBAL_RESET_YAW_ENCODER - yawMotor.Location.Location;
    ui_info.yaw_angle = ui_info.yaw_angle - (int)(ui_info.yaw_angle);
    ui_info.Vision_Success = gimbalStatus.chassisData.Vision_Success;
}
